package com.iretailer.report.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iretailer.report.dto.PageResult;
import com.iretailer.report.dto.QueryOption;
import com.iretailer.report.mapper.FaceImageMapper;
import com.iretailer.report.mapper.GenderAgeMapper;
import com.iretailer.report.model.FaceImage;
import com.iretailer.report.model.GenderAge;
import com.iretailer.report.model.GenderAgeImage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;

/**
 * @Auther: xjw
 * @Date: 2019/8/8 0008 21:23
 * @Description:
 */
@Service
public class FaceService {

    @Autowired
    private GenderAgeMapper genderAgeMapper;
    @Autowired
    private FaceImageMapper faceImageMapper;


    /**
     * 插入 GenderAge
     *
     * @param genderAges
     * @return
     */
    public boolean replaceGenderAges(List<GenderAge> genderAges) {
        genderAgeMapper.replace(genderAges);
        return true;
    }

    /**
     * 插入 GenderAge 和 图片
     *
     * @param genderAge
     * @param ImageId
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean insertGenderAgeImage(GenderAge genderAge, String ImageId) {
//        genderAgeMapper.insert(genderAge);
        genderAgeMapper.replace(Arrays.asList(genderAge));
        faceImageMapper.insert(new FaceImage(ImageId, genderAge.getImage()));
        return true;
    }

    public PageResult<GenderAgeImage> faceImageGrid(List<String> dids, LocalDate startDate, LocalDate endDate, QueryOption queryOption) {
        if (!dids.isEmpty()) {
            Page page = new Page<GenderAge>(1, Long.MAX_VALUE);
            QueryWrapper queryWrapper = new QueryWrapper();
//        queryWrapper.eq("MallId", mallId);
            queryWrapper.in("Did", dids);
            queryWrapper.between("Date", startDate, endDate);
            queryWrapper.orderByDesc("DateTime");
            queryOption.judge(page, queryWrapper);

            genderAgeMapper.faceImageGrid(page, queryWrapper);
            return new PageResult<>(page.getTotal(), page.getRecords());
        } else {
            return PageResult.emptyPageResult();
        }
    }

    public PageResult<GenderAgeImage> lasetFaceImageGrid(String mallId, LocalDate startDate, LocalDate endDate, QueryOption queryOption) {
        Page page = new Page<GenderAgeImage>(1, Long.MAX_VALUE);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryOption.judge(page, queryWrapper);
        genderAgeMapper.lasetFaceImageGrid(page, mallId, startDate, endDate);
        return new PageResult<>(page.getTotal(), page.getRecords());
    }

    public PageResult<GenderAgeImage> lastFaceImageDetailGrid(String personId, LocalDate startDate, LocalDate endDate, QueryOption queryOption) {
        Page page = new Page<GenderAge>(1, Long.MAX_VALUE);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("PersonId", personId);
        queryWrapper.between("Date", startDate, endDate);
        queryWrapper.orderByDesc("DateTime");
        queryOption.judge(page, queryWrapper);

        genderAgeMapper.faceImageGrid(page, queryWrapper);
        return new PageResult<>(page.getTotal(), page.getRecords());
    }

    public List<GenderAgeImage> selectGroupByGender(List<String> mallIds, String startDate, String endDate) {
        QueryWrapper queryWrapper = new QueryWrapper<>().between("Date", startDate, endDate).in("MallId", mallIds);
        return genderAgeMapper.selectGroupByGender(queryWrapper);
    }

    public List<GenderAgeImage> selectGroupByAgeType(List<String> mallIds, String startDate, String endDate) {
        QueryWrapper queryWrapper = new QueryWrapper<>().between("Date", startDate, endDate).in("MallId", mallIds);
        return genderAgeMapper.selectGroupByAgeType(queryWrapper);
    }

    public String getFaceImage(String imageId) {
        return faceImageMapper.getFaceImage(imageId);
    }


}
